/** \file qwwdic.h
 * \brief QWordWizard Dictionary
 */
#ifndef __QWWDIC_H
#define __QWWDIC_H

#include <QMutex>
#include <QString>
#include <vector>
#include <utility>
#include <stdexcept>

using namespace std;

/** \enum DictionaryType
 * \brief Dictionary type
 */
enum DictionaryType
{
    e_xml,                                          /**< XML */
    e_csv                                           /**< CSV */
};

/** \typedef QWWDic_attr_t
 * \brief Dictionaries attrubutes
 */
typedef vector<pair<bool, QString> > QWWDic_attr_t;

/** \typedef QWWDic_data_t
 * \brief Dictionary data
 */
typedef pair<QString, QString> QWWDic_data_t;

/** \struct QWWDic_t
 * \brief Dictionary type
 */
struct QWWDic_t
{
    bool used;                                      /**< Used flag */
    QString description;                            /**< Description */
    QString filename;                               /**< Filename */
    vector<QWWDic_data_t> data;                     /**< Data */
};

/** \class QWWDic
 * \brief QWordWizard Dictionary
 */
class QWWDic
{
    public:
        /**
         * Get instance
         * \return Instance
         */
        static QWWDic *Instance();

        /**
         * Preload dictionary
         */
        QString Preload(QString filename, DictionaryType type = e_xml) const throw (runtime_error);

        /**
         * Erase all dictionaries
         */
        void Clear();

        /**
         * Add dictionary
         * \param used Used flag
         * \param filename Filename
         */
        void Add(bool used, QString filename);

        /**
         * Get dictionaries attributes
         * \param dic Dictionaries attrubute array
         */
        void GetDictionariesAttr(QWWDic_attr_t &dic) const;

        /** Get dictionary data
         * \return data Pair of words
         */
        void GetData(QWWDic_data_t &data) const;

        /**
         * Reload dictionaries
         * \return true - ok, false - error
         */
        bool Reload();

    private:
        /**
         * Constructor
         */
        QWWDic();

        /**
         * Copy constructor
         * \param ob Object
         */
        QWWDic(const QWWDic &ob);

        /**
         * Operator =
         * \param ob Object
         */
        QWWDic& operator = (const QWWDic &ob);

        /**
         * Destructor
         */
        ~QWWDic();

        /**
         * Destroy object at exit
         */
        static void destroy();

        /**
         * Load XML dictionary
         * \param filename Filename
         */
        void LoadXML(QString filename);

        /** Load CSV dictionary
         * \param filename Filename
         */
        void LoadCSV(QWWDic_t *dic) throw (runtime_error);

    private:
        static QWWDic *mp_instance;                     /**< Instance pointer */
        static QMutex m_mutex;                          /**< Mutex */

        vector<QWWDic_t> m_dictionaries;                /**< Dictionaries array */
};

#endif
